package org.dromara.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.wms.domain.WmsBom;
import org.dromara.wms.domain.WmsBomLine;
import org.dromara.wms.domain.WmsBomProcess;
import org.dromara.wms.service.WmsBomServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/bom")
public class WmsBomController extends BaseService {

  private final WmsBomServiceImpl wmsBomService;

  /**
   * 查询物料清单列表
   */
  @SaCheckPermission("wms:bom:list")
  @GetMapping("/list")
  public TableDataInfo<WmsBom> list(WmsBom bo, PageQuery pageQuery) {
    return wmsBomService.queryPageList(bo, pageQuery);
  }

  /**
   * 导出物料清单列表
   */
  @SaCheckPermission("wms:bom:export")
  @Log(title = "测试主从", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(WmsBom bo, HttpServletResponse response) {
    List<WmsBom> list = wmsBomService.queryList(bo);
    ExcelUtil.exportExcel(list, "测试主从", WmsBom.class, response);
  }

  /**
   * 获取物料清单详细信息
   * @param id 主键
   */
  @SaCheckPermission("wms:bom:query")
  @GetMapping("/{id}")
  public R<WmsBom> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
    return R.ok(wmsBomService.queryById(id));
  }

  /**
   * 新增物料清单
   */
  @SaCheckPermission("wms:bom:add")
  @Log(title = "测试主从", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody WmsBom bo) {
    return toAjax(wmsBomService.insert(bo));
  }

  /**
   * 修改物料清单
   */
  @SaCheckPermission("wms:bom:edit")
  @Log(title = "测试主从", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody WmsBom bo) {
    return toAjax(wmsBomService.update(bo));
  }

  /**
   * 删除物料清单
   *
   * @param ids 主键串
   */
  @SaCheckPermission("wms:bom:remove")
  @Log(title = "测试主从", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
    return toAjax(wmsBomService.deleteWithValidByIds(List.of(ids), true));
  }

  /**
   * ======================Line表的路由服务==========================
   */
  /**
   * 查询物料明细列表
   */
  @SaCheckPermission("wms:bom:list")
  @GetMapping("/line/list")
  public R<List<WmsBomLine>> lineList(WmsBomLine bo) {
    return R.ok(wmsBomService.queryLineList(bo));
  }

  /**
   * 导出物料明细列表
   */
  @SaCheckPermission("wms:bom:export")
  @Log(title = "测试主从", businessType = BusinessType.EXPORT)
  @PostMapping("/line/export")
  public void lineExport(WmsBomLine bo, HttpServletResponse response) {
    List<WmsBomLine> list = wmsBomService.queryLineList(bo);
    ExcelUtil.exportExcel(list, "测试主从", WmsBomLine.class, response);
  }

  /**
   * 获取物料明细详细信息
   */
  @SaCheckPermission("wms:bom:query")
  @GetMapping("/line")
  public R<WmsBomLine> getLineInfo( WmsBomLine bo) {
    return R.ok(wmsBomService.queryLineById(bo));
  }

  /**
   * 新增物料清单明细
   */
  @SaCheckPermission("wms:bom:add")
  @Log(title = "测试主从", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping("/line")
  public R<Void> add(@Validated(AddGroup.class) @RequestBody WmsBomLine bo) {
    return toAjax(wmsBomService.insertLine(bo));
  }

  /**
   * 修改物料清单明细
   */
  @SaCheckPermission("wms:bom:edit")
  @Log(title = "测试主从", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/line")
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody WmsBomLine bo) {
    return toAjax(wmsBomService.updateLine(bo));
  }


  /**
   * 删除物料明细
   *
   */
  @SaCheckPermission("wms:bom:remove")
  @Log(title = "测试主从", businessType = BusinessType.DELETE)
  @DeleteMapping("/line")
  public R<Void> removeLine(@RequestBody WmsBomLine bo) {
    return toAjax(wmsBomService.deleteLineWithValid(bo, true));
  }

  /**
   * ======================关联表的路由服务==========================
   */
  /**
   * 查询物品工序列表
   */
  @SaCheckPermission("wms:bom:list")
  @GetMapping("/process/list")
  public TableDataInfo<WmsBomProcess> processList(WmsBomProcess bo, PageQuery pageQuery) {
    return wmsBomService.queryProcessPageList(bo, pageQuery);
  }

  /**
   * 新增物料清单明细
   */
  @SaCheckPermission("wms:bomProcess:add")
  @Log(title = "Bom_Process新增", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping("/process")
  public R<Void> processAdd(@Validated(AddGroup.class) @RequestBody WmsBomProcess bo) {
    return toAjax(wmsBomService.insertProcess(bo));
  }

  /**
   * 修改物料清单明细
   */
  @SaCheckPermission("wms:bomProcess:edit")
  @Log(title = "Bom_Process修改", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/process")
  public R<Void> processEdit(@Validated(EditGroup.class) @RequestBody WmsBomProcess bo) {
    return toAjax(wmsBomService.updateProcess(bo));
  }

  /**
   * 导出物品工序列表
   */
  @SaCheckPermission("wms:bom:export")
  @Log(title = "测试主从", businessType = BusinessType.EXPORT)
  @PostMapping("/process/export")
  public void processExport(WmsBomProcess bo, HttpServletResponse response) {
    List<WmsBomProcess> list = wmsBomService.queryProcessList(bo);
    ExcelUtil.exportExcel(list, "测试主从", WmsBomProcess.class, response);
  }

  /**
   * 获取物品工序详细信息
   */
  @SaCheckPermission("wms:bom:query")
  @GetMapping("/process")
  public R<WmsBomProcess> getProcessInfo( WmsBomProcess bo) {
    return R.ok(wmsBomService.queryProcessById(bo));
  }


  /**
   * 删除物品工序
   *
   */
  @SaCheckPermission("wms:bom:remove")
  @Log(title = "测试关联", businessType = BusinessType.DELETE)
  @DeleteMapping("/process")
  public R<Void> removeProcess(@RequestBody WmsBomProcess bo) {
    return toAjax(wmsBomService.deleteProcessWithValid(bo, true));
  }
}
